// components/bindingPhone/index.js
import Toast from '@vant/weapp/toast/toast';
import Dialog from '@vant/weapp/dialog/dialog';
const $api = require("../../request/api").API
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    show:{
      type:Boolean,
      observer(val){
      }
    },
    phone:{
      type:String,
      observer(val){
        this.setData({
          realPhone:val
        })
      }
    },
    smsType:{
      type:String,
    },
    userType:{
      type:Number
    },
    title:{
      type:String
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    realPhone: '',
    inputPhone:"",
    second:"",
    code:"",
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 获取验证码
  async getCode() {
    const { inputPhone,smsType,useType } = this.data
    const reg = /^1(3|4|5|6|7|8|9)\d{9}$/
    if (!inputPhone) {
      Toast.fail("请填写手机号")
      return
    }
    if (!reg.test(inputPhone)) {
      Toast.fail("手机号码格式不正确")
      return
    }
    const params = {
      mobile: inputPhone,
      smsType,
      useType,
    }
    this.setData({
      second: 60
    })
    this.timer = setInterval(() => {
      this.setData({
        second: this.data.second - 1
      })
      if (this.data.second == 0) {
        clearInterval(this.timer)
      }
    },1000)
    let res = await $api.getPhoneCode(params)
    if (res.data.status == 0) {
      wx.showToast({
        title: '发送验证码成功',
        status: 'success'
      })

    }

  },
  onInput(e){
    const prop = e.currentTarget.dataset.prop
    this.setData({
      [prop]: e.detail.value
    })
  },
  onDelete(e){
    const prop = e.currentTarget.dataset.prop
    this.setData({
      [prop]: ""
    })
  },
  close(){
    this.triggerEvent('cancel')
    // this.setData({
    //   show:false
    // })
    // console.log(this.data.show)
  },
  confirm(){
    const reg = /^1(3|4|5|6|7|8|9)\d{9}$/
    const {inputPhone,code} = this.data
    if (!inputPhone) {
      Toast.fail("请填写手机号")
      return
    }
    if (!reg.test(inputPhone)) {
      Toast.fail("手机号格式错误")
      return
    }
    if (!code) {
      Toast.fail("请填写验证码")
      return
    }
    this.triggerEvent("confirm",{phone:inputPhone,code})
  }
  }
})
